package com.tjvib.presenter;

import static com.tjvib.Constants.SUCCESS;

import com.tjvib.bean.DataSet;
import com.tjvib.common.Callback;
import com.tjvib.sensor.LocalSensorData;
import com.tjvib.util.FileUtil;
import com.tjvib.util.LogUtil;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.util.ToastUtil;
import com.tjvib.util.retrofit.BaseResponse;
import com.tjvib.util.retrofit.RetrofitHelper;
import com.tjvib.view.activity.MainActivity;
import com.tjvib.widget.AndroidScheduler;

import org.json.JSONException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainPresenter extends BasePresenter {

    private static final String TAG = "RootPresenter";

    public MainPresenter(MainActivity view) {
        super(view);
    }

    public boolean checkTempDataSetExists() {
        return FileUtil.isDataSetTempFileExists(view);
    }

    public String getTempDataSetType() {
        return FileUtil.getTempDataSetType(view);
    }

    public void storeTempDataSet(String dataSetName, Callback callback) {
        new Thread(() -> {
            DataSet dataSet = FileUtil.getTempDataSet(view, dataSetName);
            if (dataSet == null) {
                callback.onFail("Read temp file failed", null);
                return;
            }
            String type = dataSet.getInfo().getType();
            String dataSetStr = null;
            try {
                dataSetStr = dataSet.toJsonString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), dataSetStr);
            if (type.equals("lpmsb2")) {
                RetrofitHelper.getInstance().create()
                        .collectLpmsB2("" + UserManager.getInstance().getUserId(), requestBody)
                        .subscribe(new Observer<BaseResponse>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(BaseResponse value) {
                                if (value.getCode() == SUCCESS) {
                                    callback.onSuccess(value.getMessage(), null);
                                    UserManager.getInstance().setInfo(String.valueOf(value.getData()));
                                    FileUtil.deleteDataSetTempFile(view);
                                } else {
                                    callback.onFail(value.getMessage(), null);
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                ToastUtil.show(e.getMessage());
                                LogUtil.e("lpmsb2 " + e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            } else if (type.equals("local")) {
                RetrofitHelper.getInstance().create()
                        .collectLocal("" + UserManager.getInstance().getUserId(), requestBody)
                        .subscribe(new Observer<BaseResponse>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(BaseResponse value) {
                                if (value.getCode() == SUCCESS) {
                                    callback.onSuccess(value.getMessage(), null);
                                    UserManager.getInstance().setInfo(String.valueOf(value.getData()));
                                    FileUtil.deleteDataSetTempFile(view);
                                } else {
                                    callback.onFail(value.getMessage(), null);
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                ToastUtil.show(e.getMessage());
                                LogUtil.e("local " + e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        }).start();
    }

    public void discardTempDataSet() {
        FileUtil.deleteDataSetTempFile(view);
    }

    @Override
    String getTag() {
        return TAG;
    }
}
